package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.enums.EquipmentEnums;
import com.mxpio.erp.common.equipment.entity.ProjectAssistingPersonnel;
import com.mxpio.erp.common.equipment.entity.ProjectManagement;
import com.mxpio.erp.common.equipment.service.ProjectAssistingPersonnelService;
import com.mxpio.erp.common.equipment.service.ProjectManagementService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.security.entity.User;
import com.mxpioframework.security.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "ProjectAssistingPersonnelController", description = "协助人员列表")
@RestController
@RequestMapping("/erp/equipment/project/assisting/personnel/")
public class ProjectAssistingPersonnelController {

    @Autowired
    private ProjectAssistingPersonnelService projectAssistingPersonnelService;

    @Autowired
    private ProjectManagementService projectManagementService;

    @Autowired
    private UserService userService;

    @GetMapping("list")
    @Operation(summary = "协助人员列表", description = "获取协助人员列表(无分页)", method = "GET")
    public Result<List<ProjectAssistingPersonnel>> list(@Parameter(description = "查询参数")Criteria criteria){
        List<ProjectAssistingPersonnel> list = projectAssistingPersonnelService.list(ProjectAssistingPersonnel.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "协助人员列表", description = "获取协助人员列表(分页)", method = "GET")
    public Result<Page<ProjectAssistingPersonnel>> page(@Parameter(description = "查询参数")Criteria criteria, @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页数") Integer pageNo){
        PageRequest page = PageRequest.of(pageNo - 1, pageSize);
        Page<ProjectAssistingPersonnel> listPage = projectAssistingPersonnelService.listPage(ProjectAssistingPersonnel.class, page, criteria);
        return Result.OK(listPage);
    }

    @GetMapping("list/{proCode}")
    @Operation(summary = "根据proCode获取协助人员列表", description = "根据proCode获取协助人员列表", method = "GET")
    public Result<ProjectAssistingPersonnel> getById(@PathVariable(name = "proCode", required = true) String proCode){
        ProjectAssistingPersonnel byId = projectAssistingPersonnelService.getById(ProjectAssistingPersonnel.class, proCode);
        return Result.OK(byId);
    }

    @RequestMapping("add/{proCode}")
    @Operation(summary = "保存协助人员列表", description = "保存协助人员列表", method = "POST")
    public Result<ProjectAssistingPersonnel> add(@PathVariable(name = "proCode", required = true) String proCode, @RequestBody ProjectAssistingPersonnel projectAssistingPersonnel){
        ProjectManagement projectManagement = projectManagementService.getById(ProjectManagement.class, proCode);
        String proStatus = projectManagement.getProStatus();
        if (proStatus.equals(EquipmentEnums.Project.IMPROGRESS.getCode())){
            ProjectAssistingPersonnel save = projectAssistingPersonnelService.save(projectAssistingPersonnel);
            return Result.OK(save);
        }else {
            return Result.error("该项目不在执行状态, 禁止添加协助人员");
        }
    }

    @GetMapping("getByAssistantUserId/{username}")
    @Operation(summary = "根据工号获取协助人员", description = "根据工号获取协助人员", method = "GET")
    public Result<User> getByAssistantUserId(@PathVariable(name = "username", required = true) String username){
        User byName = userService.findByName(username);
        return Result.OK(byName);
    }

    @PutMapping("edit")
    @Operation(summary = "保存协助人员列表", description = "保存协助人员列表", method = "PUT")
    public Result<ProjectAssistingPersonnel> edit(@RequestBody ProjectAssistingPersonnel projectAssistingPersonnel){
        ProjectAssistingPersonnel update = projectAssistingPersonnelService.update(projectAssistingPersonnel);
        return Result.OK(update);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除协助人员列表", description = "根据id删除协助人员列表,多个人员用英文逗号分隔", method = "DELETE")
    public Result<ProjectAssistingPersonnel> remove(@PathVariable(name = "ids", required = true)String ids){
        String[] id = ids.split(",");
        for (String key : id) {
            projectAssistingPersonnelService.remove(ProjectAssistingPersonnel.class, key);
        }
        return Result.OK();
    }


}
